﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class UpdateAcct : Form
    {
        public UpdateAcct()
        {
            InitializeComponent();
            load_table();
        }

        private void UpdateAcct_Load(object sender, EventArgs e)
        {          

        }

        

        #region RefreshDataGridView
        void load_table()
        {
            string constring = "Data Source=LUCKYPX\\LUCKYPX;Initial Catalog=SWENAssignment3;User ID=sa;Password=imsa";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand("SELECT Staff_Account_ID,Staff_Position, First_Name,Last_Name,Duty_Type FROM Staff", conDataBase);
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                sda.Update(dbdataset);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            string constring = "Data Source=LUCKYPX\\LUCKYPX;Initial Catalog=SWENAssignment3;User ID=sa;Password=imsa";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand("SELECT Staff_Account_ID,Staff_Position, First_Name,Last_Name,Duty_Type FROM Staff", conDataBase);
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmdDataBase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bSource = new BindingSource();

                bSource.DataSource = dbdataset;
                dataGridView1.DataSource = bSource;
                sda.Update(dbdataset);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion RefreshDataGridView

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (cbxPos.Text == String.Empty || txbFirst.Text == String.Empty || txbLast.Text == String.Empty || cbxDuty.Text == String.Empty)
            {
                MessageBox.Show("Pleasae fill up all required information.");
            }
            else
            {
                Account newAcct = new Account();

                newAcct.staffId = lblAcctId.Text.ToString(); ;
                newAcct.staffPosition = cbxPos.Text;
                newAcct.firstName = txbFirst.Text.ToString();
                newAcct.lastName = txbLast.Text.ToString();
                newAcct.dutyType = cbxDuty.Text;

                AccountDBManager.UpdateAccount(newAcct);

                load_table();
            }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            cbxPos.Text = null;
            txbFirst.Text = null; ;
            txbLast.Text = null;
            cbxDuty.Text = null;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row=this.dataGridView1.Rows[e.RowIndex];

                lblAcctId.Text = row.Cells[0].Value.ToString();
                cbxPos.Text = row.Cells[1].Value.ToString();
                txbFirst.Text = row.Cells[2].Value.ToString(); ;
                txbLast.Text = row.Cells[3].Value.ToString();
                cbxDuty.Text = row.Cells[4].Value.ToString();

            }
        }

        #region toolStrip
        UpdateAcct updateAcctChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (updateAcctChild == null)
            {
                updateAcctChild = new UpdateAcct();
                updateAcctChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                updateAcctChild.MdiParent = this.MdiParent;
                // Display the form.
                updateAcctChild.Show();
                updateAcctChild.Activate();
            }
        }

        CreateNewAcct createAcctChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (createAcctChild == null)
            {
                createAcctChild = new CreateNewAcct();
                createAcctChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                createAcctChild.MdiParent = this.MdiParent;
                // Display the form.
                createAcctChild.Show();
                createAcctChild.Activate();
            }
        }

        DeleteAcct deleteAcct;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (deleteAcct == null)
            {
                deleteAcct = new DeleteAcct();
                deleteAcct.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                deleteAcct.MdiParent = this.MdiParent;
                // Display the form.
                deleteAcct.Show();
                deleteAcct.Activate();
            }
        }
        #endregion toolStrip

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                updateAcctChild = null;
                createAcctChild = null;
                deleteAcct = null;
            }
        }
        #endregion CloseAllForms



    }
}
